#include <tsin/runtime/SpellEngine.hpp>

#include <tsin/runtime/GameRuntime.hpp>
#include <tsin/input/Input.hpp>

#include <tsin/util/EventMediator.hpp>
#include <tsin/util/EventSource.hpp>

#include <cstdlib>
#include <ctime>

#include <tsin/util/MemCheck.hpp>
using namespace tsin;

SpellEngine::SpellEngine()
{
        srand((unsigned int)time(0));
        works = new Work*[20];
}

SpellEngine::~SpellEngine()
{
        delete[] works;
}

void SpellEngine::init(GameRuntime *runtime)
{
        done = false;
        int i = 0;

        works[i++] = &inputHandle;
        works[i++] = &gameEventHandle;
        works[i++] = &displayReset;
        works[i++] = &earlyCharacterBlit;
        works[i++] = &bulletBlit;
        works[i++] = &lateCharacterBlit;
        works[i++] = &frontBlit;
        works[i++] = &displayUpdate;
        works[i++] = &timerDelay;
        works[i++] = 0;

        runtime->input.registerListener(this, 0);
}

int SpellEngine::handle(EventSource *source, int eventNo)
{
        InputEvent *event = static_cast<InputEvent *>(source->getEventInfo());
        switch (event->type) {
        case INPUT_EVENT_GAME_CTRL:
                if (event->data.gameControl.event == GAME_CTRL_EXIT)
                        done = true;
                break;
        }
        return 0;
}
